#include <stdlib.h>
#include <stdio.h>

#include "sprites.h"

struct sprite
{
  sprite_type active;
  unsigned char* sprite8;
  unsigned short* sprite16;
  unsigned long* sprite32;
};

void SPRsetType(sprite spr, sprite_type type)
{
	spr->active = type;
}

sprite_type SPRgetType(sprite spr) { return spr->active; }

sprite SPRnew(sprite_type type)
{
  sprite s = malloc(sizeof(struct sprite));
  s->active = type;
  s->sprite8 = NULL;
  s->sprite16 = NULL;
  s->sprite32 = NULL;
  return s;
}

sprite SPRcopy(sprite spr)
{
	sprite nspr = SPRnew(spr->active);
	SPRsetSprite8(nspr, spr->sprite8);
	SPRsetSprite16(nspr, spr->sprite16);
	SPRsetSprite32(nspr, spr->sprite32);
	return nspr;
}

void SPRfree(sprite skin)
{
	if( skin != NULL )
	{
		if( skin->sprite8 )
			free( skin->sprite8 );
		if( skin->sprite16 )
			free( skin->sprite16 );
		if( skin->sprite32)
			free( skin->sprite32 );
		free(skin);
	}
}

void SPRsetSprite8(sprite skin, unsigned char image[16])
{
  skin->sprite8 = malloc(16 * sizeof( unsigned char ));
  short i;
  for( i = 0; i < 16; i++ )
  {
    skin->sprite8[i] = image[i];
  }
}

void SPRsetSprite16(sprite skin, unsigned short image[32])
{
  skin->sprite16 = malloc(32 * sizeof( unsigned short ));
  short i;
  for( i = 0; i < 32; i++ )
  {
    skin->sprite16[i] = image[i];
  }
}

void SPRsetSprite32(sprite skin, unsigned long image[64])
{
  skin->sprite32 = malloc(64 * sizeof( unsigned long ));
  short i;
  for( i = 0; i < 64; i++ )
  {
    skin->sprite32[i] = image[i];
  }
}

void SPRblit(sprite skin, int x, int y)
{
  switch(skin->active)
  {
    case SPRITE8:
      Sprite8(x, y, 8, skin->sprite8, GrayGetPlane(DARK_PLANE), SPRT_XOR);
      Sprite8(x, y, 8, skin->sprite8 + 8, GrayGetPlane(LIGHT_PLANE), SPRT_XOR);
      break;
    case SPRITE16:
      Sprite16(x, y, 16, skin->sprite16, GrayGetPlane(DARK_PLANE), SPRT_XOR);
      Sprite16(x, y, 16, skin->sprite16 + 16, GrayGetPlane(LIGHT_PLANE), SPRT_XOR);
      break;
    case SPRITE32:
      Sprite32(x, y, 32, skin->sprite32, GrayGetPlane(DARK_PLANE), SPRT_XOR);
      Sprite32(x, y, 32, skin->sprite32 + 32, GrayGetPlane(LIGHT_PLANE), SPRT_XOR);
      break;
  }
}
